// packageStore/pages/profile/invoice/list.js
import {
  api,
  util, 
  clearIncrement
} from '../../../../js/util'; 
import {
  invoice
} from '../../../../js/modules/store';
var app = getApp();
import BasePage from '../../../../js/common/base-page'
BasePage({
  pageKey:"invoiceList",

  /**
   * 页面的初始数据
   */
  data: {
    isIphoneX:app.globalData.isIphoneX,
    mainColor:api.titleColor||"#e56b14",//主题色
    invoiceId: 0,
    invoiceList: [],
    keyword:'',//关键词搜索
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that=this;
    if(options.id>0){
      that.setData({
        invoiceId:options.id
      })
    };
    this.invoiceAddCom = this.selectComponent("#invoiceAddCom");
    this.loadInvoiceList();
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    clearIncrement();
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  /**加载发票列表 */
  loadInvoiceList() {
    let that = this;
    invoice.invoiceList(0,that.data.keyword,function (content) {
      that.setData({
        invoiceList: content
      });
    });
  },
  //添加发票
  addInvoice(){
    var invoiceInfo = {
      Id: 0,
      InvType: 2,
      Name: '',
      Code: '',
      Address: '',
      Tel: '',
      Bank: '',
      Account: '',
      PostAddress: '',
      Status: 1,
      IsDefault: 0,
      IsInvoiceType: true
    }
    this.invoiceAddCom.showAddModal(invoiceInfo);
  },
  /**编辑发票 */
  editInvoice(e) {
    var idx = e.currentTarget.dataset.idx;
    var invoiceList = this.data.invoiceList;
    var invoiceItem = invoiceList[idx];
    invoiceItem.IsInvoiceType = false;
    this.invoiceAddCom.showAddModal(invoiceItem);
  },
  /**删除发票 */
  deleteInvoice(evt) {
    var that = this;
    util.confirm(function () {
      var id = evt.currentTarget.dataset.id;
      invoice.deleteInvoice(id, function (status, res) {
        if (status) {
          util.success("删除成功", () => {
            that.loadInvoiceList();
          });
        } else {
          util.tips(res||"删除失败");
        }
      });
    }, null, "您确定要删除此发票信息吗?"," ");
  },
  /**发票回调 */
  onInvoiceCallbackEvent(e){
    this.loadInvoiceList();
  },
  /**
   * 关键词搜索
   */
  onSearchKeywordEvent: function(e) {
    var that = this;
    this.setData({ keyword:e.detail }, () => {
      that.loadInvoiceList();
    });
  },
})